package com.ihisomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ihisomp.base.Page;
import com.ihisomp.base.Result;
import com.ihisomp.dto.OrgRoleDto;
import com.ihisomp.dto.SysPathTreeDto;
import com.ihisomp.entity.OrgRole;
import com.ihisomp.entity.OrgRolePage;
import com.ihisomp.mapper.OrgRoleMapper;
import com.ihisomp.service.IOrgRolePageService;
import com.ihisomp.service.IOrgRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihisomp.service.ISysGetNoService;
import com.ihisomp.util.ComUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色信息 服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-04-12
 */
@Service
public class OrgRoleServiceImpl extends ServiceImpl<OrgRoleMapper, OrgRole> implements IOrgRoleService {

    @Autowired
    private ISysGetNoService sysGetNoService;

    @Autowired
    private IOrgRolePageService orgRolePageService;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param req
     * @return
     */
    @Override
    public Result search(long page, long size, OrgRoleDto req) {
        //  查询条件
        QueryWrapper<OrgRole> query = new QueryWrapper<>();
        query.like("org_key", req.getOrgKey());
        if (req != null) {
            if (StringUtils.isNotBlank(req.getRoleName())) {
                query.like("role_name", req.getRoleName());
            }
        }
        query.orderByAsc("seq_no");
        // 分页查询
        IPage<OrgRole> data = baseMapper.selectPage(new Page<>(page, size), query);
        return Result.ok(data);
    }

    /**
     * 新增方法
     *
     * @param orgRoleDto
     * @return
     */
    @Override
    public Result add(OrgRoleDto orgRoleDto) {
        if (orgRoleDto == null || StringUtils.isBlank(orgRoleDto.getRoleName())) {
            return Result.error("角色名称不能为空！");
        }
        OrgRole orgRole = new OrgRole();
        BeanUtils.copyProperties(orgRoleDto, orgRole);
        orgRole.setRoleKey(sysGetNoService.getSysNo("t_org_role"));
        if (baseMapper.insert(orgRole) > 0) {
            return Result.ok();
        }
        return Result.error("新增角色失败！");
    }

    /**
     * 删除方法
     *
     * @param roleKey
     * @return
     */
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    @Override
    public Result delete(String roleKey) {
        if (baseMapper.deleteById(roleKey) > 0) {
            if (deleteOrgRolePage(roleKey)) {
                return Result.ok();
            }
        }
        return Result.error("删除角色失败！");
    }

    /**
     * 更新方法
     *
     * @param roleKey
     * @param orgRoleDto
     * @return
     */
    @Override
    public Result update(String roleKey, OrgRoleDto orgRoleDto) {
        if (orgRoleDto.getRoleKey() == null) {
            orgRoleDto.setRoleKey(roleKey);
        }
        OrgRole orgRole = new OrgRole();
        BeanUtils.copyProperties(orgRoleDto, orgRole);
        if (baseMapper.updateById(orgRole) > 0) {
            return Result.ok();
        }
        return Result.error("更新角色失败！");
    }

    /**
     * 查询方法
     *
     * @param roleKey
     * @return
     */
    @Override
    public Result get(String roleKey) {
        OrgRoleDto orgRoleDto = new OrgRoleDto();
        BeanUtils.copyProperties(baseMapper.selectById(roleKey), orgRoleDto);
        orgRoleDto.setSysPageList(orgRolePageService.getPageByRoleKey(roleKey));
        return Result.ok(orgRoleDto);
    }

    /**
     * 查询角色列表
     *
     * @param orgRoleDto
     * @return
     */
    @Override
    public Result selectList(OrgRoleDto orgRoleDto) {
        QueryWrapper<OrgRole> query = new QueryWrapper<>();
        query.like("org_key", orgRoleDto.getOrgKey());
        List<OrgRole> roles = baseMapper.selectList(query);
        return Result.ok(roles);
    }

    /**
     * 更新角色权限
     *
     * @param roleKey
     * @param orgRoleDto
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public Result updateRolePath(String roleKey, OrgRoleDto orgRoleDto) {
        if (orgRoleDto.getSysPageList() == null || orgRoleDto.getSysPageList().size() == 0) {
            return Result.error("角色权限不能为空！");
        }
        if (!deleteOrgRolePage(roleKey)) {
            return Result.error("更新角色失败！");
        }
        if (!addOrgRolePage(roleKey, orgRoleDto.getSysPageList())) {
            return Result.error("更新角色失败！");
        }
        return Result.ok();
    }

    /**
     * 批量新增角色程序路径
     *
     * @param roleKey
     * @param pathList
     * @return
     */
    private boolean addOrgRolePage(String roleKey, List<SysPathTreeDto> pathList) {
        OrgRolePage orgRolePage = new OrgRolePage();
        orgRolePage.setRoleKey(roleKey);
        for (int i = 0; i < pathList.size(); i++) {
            if ("ROOT".equals(pathList.get(i).getId())) {
                continue;
            }
            orgRolePage.setRolePagesKey(ComUtil.createPrimaryKey());
            orgRolePage.setPagePathKey(pathList.get(i).getId());
            if (!orgRolePageService.save(orgRolePage)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 批量删除角色程序路径
     *
     * @param roleKey
     * @return
     */
    private boolean deleteOrgRolePage(String roleKey) {
        if (orgRolePageService.deleteByRoleKey(roleKey) > -1) {
            return true;
        }
        return false;
    }
}
